package cn.yili12.server.info.handler;

import cn.yili12.server.cache.OnlineUserCache;
import cn.yili12.server.info.vo.InfoMessage;
import cn.yili12.server.info.vo.InfoMessageType;
import cn.yili12.server.mq.QueueProducer;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 通知在线
 */
@Slf4j
@Component
public class OnlineInfoHandler extends BaseInfoHandler {
    @Autowired
    protected QueueProducer producer;
    @Autowired
    protected OnlineUserCache onlineUserCache;

    @Override
    public boolean handler(InfoMessage message) {
        //通知所有人  我上线了
        webSocketChannelPool.broadcast(message.getFrom(), JSON.toJSONString(message));

 /*       //获取所有在线用户并给客户端
        List<String> onlineList = webSocketChannelPool.getOnlineList();
        //过滤掉自己
        List<String> list = onlineList.stream().filter((item) -> {
            return !item.equals(message.getFrom());
        }).collect(Collectors.toList());
        InfoMessage infoMessage = new InfoMessage();
        infoMessage.setNo(String.valueOf(idTool.createId()));
        infoMessage.setType(InfoMessageType.OnlineList);
        infoMessage.setTo(message.getFrom());
        infoMessage.setData(JSON.toJSONString(list));
        infoMessage.setFrom("");
        //如果发送给客户端失败就放入消息队列
        if (!webSocketChannelPool.sendTo(message.getFrom(), JSON.toJSONString(infoMessage))) {
            producer.send(properties.getInfoQueue(), JSON.toJSONString(infoMessage));
        }
        onlineUserCache.add(message.getFrom());*/
        return true;
    }
}
